/**
 * Created by Administrator on 2018/4/3.
 */
layui.use(['form'], function () {
    var form = layui.form
        , layer = layui.layer;

    function initPage() {
        var token = sessionStorage.access_token;
        if(token==null){
            againToken();
            return;
        }
        //限高
        $('.my-challenge-tab-content').height(winH - 85 + 'px');

        var vue = new Vue({
            el: '#index_div',
            data: {
                data_list: {},  //提现记录
                next_page: 1,// 下一页的页码
                allpage: 1,//总页码，会从后台获取
                page_size: 5, //每页条数
            },
            watch: {},
            methods: {

            },
            filters: {
                timestampToTime: function (val) {
                    return formatDate(val);
                },
                integral: function (value) {
                    return parseInt(value);
                },
                addMoney:function([money,poundage]){
                    var total = parseFloat(money)+parseFloat(poundage);
                    return total.toFixed(2);
                }

            }
        });

        /**
         * 滚动事件
         */
        function scrollFn() {
            //真实内容的高度
            var pageHeight = Math.max(document.body.scrollHeight, document.body.offsetHeight);
            //视窗的高度
            var viewportHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0;
            //隐藏的高度
            var scrollHeight = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
            if (pageHeight - viewportHeight - scrollHeight < 20) {  //如果满足触发条件，执行
                if (vue.next_page <= vue.allpage) {
                    initDataList();
                }
            }
        }

        $('.my-challenge-tab-content').bind("scroll", scrollFn);    //绑定滚动事件

        /**
         * 初始化挑战卡明细
         */
        function initDataList() {
            var data = {
                token: token,
                page_size: vue.page_size,
                page_num: vue.next_page,
            };
            //获取挑战列表信息
            $.ajax({
                type: "post"
                , url: '/index/user/ajax_withdraw_log'
                , data: data
                , async: false
                , success: function (result) {
                    console.log(result);
                    if (result.code == 0) {
                        againToken();
                        return;
                    }
                    if (result.code == 1) {

                        if (result.data.page.pagenum == "1") {
                            vue.data_list = result.data.list;
                        } else {
                            vue.data_list.push.apply(vue.data_list, result.data.list);
                        }
                        vue.next_page = parseInt(result.data.page.pagenum) + 1;
                        vue.allpage = result.data.page.totalpage;
                    }
                }
                , error: function (resq) {
                    console.info(resq);
                    layer.msg("服务器异常");
                }
            });
        }

        initDataList();

    };




    /**
     * 初始化完成加载函数
     */
    $(function () {
        initGlobalPage();
        initPage();
    })

});


